package com.glory.dmegcpda.view

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import androidx.databinding.DataBindingUtil
import com.glory.dmegcpda.R
import com.glory.dmegcpda.databinding.ActivityLotInventoryBinding
import com.glory.dmegcpda.viewmodel.LotInventoryViewModel
import com.glory.mvvmnet.base.BaseActivity
import com.glory.mvvmnet.entity.ToastMessageBean
import kotlinx.android.synthetic.main.activity_lot_inventory.*

/**
 * FileName: LotInventoryActivity
 * Author: XJH
 * Date: 2021/10/20 17:07
 * Description:
 * History:
 */
class LotInventoryActivity : BaseActivity<LotInventoryViewModel>(), View.OnClickListener {
    private var mBinding : ActivityLotInventoryBinding? = null
    override fun initData() {
        ivBack.setOnClickListener(this)
        btnSubmit.setOnClickListener(this)
        viewModel.defUI.callObserve.observe(this){
            etLot.setText("")
            etLot.requestFocus()
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        mBinding = DataBindingUtil.setContentView(this,R.layout.activity_lot_inventory)
        mBinding?.lotInventoryViewModel = viewModel
        etLot.setText("")
        etLot.requestFocus()
        etLot.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun afterTextChanged(text: Editable) {
                val toString = text.toString()
                if (toString.indexOf("\r") >= 0 || toString.indexOf("\n") >= 0) {
                    etLot.setText(toString.replace("\r", "").replace("\n", ""))
                    viewModel.getLotInfo(etLot.text.toString().trim())
                }
            }
        })

    }

    override fun layoutId(): Int {
        return R.layout.activity_lot_inventory
    }

    override fun onClick(p: View?) {
        when(p?.id){
            R.id.ivBack -> finish()
            R.id.btnSubmit ->{
                if (etLot.text.toString().isEmpty()){
                    viewModel.defUI.toastEvent.value = ToastMessageBean("请先扫描托盘号",false)
                    return
                }
                viewModel.inventory(etLot.text.toString().trim())
            }
        }
    }
}